package com.summer.jdbc.util;

import java.sql.*;

public class DataBaseUtil {
        private static final String URL = "jdbc:mysql://123.249.18.86:3306/elm-admin?characterEncoding=utf-8&useSSL=false";
        private static final String DRIVER = "com.mysql.jdbc.Driver";
        private static final String USERNAME = "root";
        private static final String PASSWORD = "123456";

        //获取Connection
        public static Connection getConnection() {
            Connection con = null;
            try {
                Class.forName(DRIVER);//检测数据库的driver是否存在
                con = DriverManager.getConnection(URL,USERNAME,PASSWORD);//通过TCP建立并获取连接
            } catch (Exception e) {
                e.printStackTrace();
            }
            return con;
        }

        //关闭资源
        public static void close(ResultSet rs, PreparedStatement pst, Connection con) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs = null;
            }
            if (pst != null) {
                try {
                    pst.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                pst = null;
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                con = null;
            }
        }
}
